package com.zhuss.vent.processor;

import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.vent.vent.CreateVentAppendBo;
import com.zhuss.bean.bo.vent.vent.wechat.ForwardVentBo;
import com.zhuss.bean.vo.vent.vent.DetailVentAppendVo;
import com.zhuss.bean.vo.vent.vent.DetailVentVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.exception.WpsException;
import com.zhuss.util.JsonUtil;

/**
 * 转发匿名吐槽 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/28 16:01
 */
@Component("ventForward")
public class VentForward extends AbstractProcessor<Object, ForwardVentBo> implements Processor<Object, ForwardVentBo> {

    @Autowired
	@Qualifier("ventRead")
	private VentRead ventRead;
    
    @Autowired
	@Qualifier("ventAppendRead")
	private VentAppendRead ventAppendRead;
    
    @Autowired
	@Qualifier("ventAppendCreate")
	private Processor<Object, CreateVentAppendBo> ventAppendCreate;
    
    /**
     * 校验
     */
    @Override
    public void check(ForwardVentBo bo) throws WpsException {
    	Long ventId = bo.getVentId();
    	Integer appendType = bo.getAppendType();
    	
    	DetailVentAppendVo result = ventAppendRead.getByType(appendType, ventId, bo.getConcurrentUserId());
    	
    	if (result != null) {
    		throw new WpsException(ExceptionCode.匿名吐槽已转发);
    	}
    }
    
    /**
     * 操作方法
     * @throws WpsException 
     */
    @Override
    public Object process(ForwardVentBo param) throws WpsException {
    	
    	Long ventId = param.getVentId();
    	DetailVentVo vent = ((DetailVentVo) ventRead.detail(ventId));
    	String name = vent.getTitle();
    	
    	CreateVentAppendBo bo = new CreateVentAppendBo();
    	BeanUtils.copyProperties(param, bo);
    	BeanUtils.copyProperties(vent, bo);
    	bo.setVentId(ventId);
    	bo.setType(param.getAppendType());
    	bo.setTitle(vent.getTitle());
    	bo.setContent(vent.getContent());
    	bo.setPic(vent.getPic());
    	ventAppendCreate.process(bo);
    	
    	
    	
    	param.setName(name);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(ForwardVentBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.匿名吐槽.key());
    	record.setAction(LogActionEnum.匿名吐槽转发.key());
    	record.setTargetId(param.getVentId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
